package main

import (
	"errors"
	"fmt"
	"os"

	. "gitee.com/cloudapex/Ghost/utd"
)

const (
	MAKE_FOR_GO     = "go"
	MAKE_FOR_CSHARP = "cs"
	MAKE_FOR_CPP    = "cc"
	MAKE_FOR_GDS    = "gd"
)
const (

	// 消息类型文件
	MESSAGE_TYPE_FILE = "MessageId.h"

	// proto协议文件包语法前缀
	PROTO_PACKAGE_PREFIX = "package "

	// 消息类型全大写 前缀
	MESSAGE_ID_PREFIX     = "MT_"
	MESSAGE_STRUCT_PREFIX = "Msg"

	//.proto文件相关名称
	FILE_NAME_PROTO      = "Protocols"
	FILE_PROTO_SUFFIX    = "proto"
	FILE_PB_PROTO_SUFFIX = ".pb.go"
)

var (
	TargetFile       string // 目标文件
	TargetFileSuffix string // 目标文件后缀

	LookUpDirNames = make([]string, 1, 10) // 遍历出的目录名

	MakeProtoHandleFun ProtoMakeHandler // 处理函数指针

	// 协议类型 和 协议结构
	MsgTypeNames   map[string]int // 消息类型字符串
	MsgTypeLines   []string       // 在MessageId.h文件里面的有效的行拷贝
	MsgStructNames []string       // 消息结构体名称

	MsgTypeLinesAll   []string = make([]string, 0, 100) // 所有MessageId.h文件里面的有效的行拷贝汇总
	MsgStructNamesAll []string = make([]string, 0, 100) // 所有消息结构名

	// 所有目录
	InterStructNamesAll []string = make([]string, 0, 100) // 内部结构名称 for gd

	MergedTempProtoFile *os.File // 临时用的合并后的proto文件
)

type ProtoMakeHandler func() error

// 初始化编译环境
func InitBuildEnviron() error {
	switch vTargetLang {
	case "go":
		TargetFileSuffix = ".go"
		MakeProtoHandleFun = MakeProtoGo
	case "cs":
		TargetFileSuffix = ".cs"
		MakeProtoHandleFun = MakeProtoGo //MakeProtoCs
	case "cc":
		TargetFileSuffix = ".h"
		MakeProtoHandleFun = MakeProtoGo //MakeProtoCc
	case "gd":
		TargetFileSuffix = ".gd"
		MakeProtoHandleFun = MakeProtoGD //MakeProtoCc
	default:
		return errors.New(fmt.Sprintf("不支持编译的语言[%s].", vTargetLang))
	}
	Util.Cast(vOutputsDir != "", func() { vOutputsDir += "/" }, nil)
	os.MkdirAll("./"+vOutputsDir, 0666)
	TargetFile = fmt.Sprintf("./%s%s%s", vOutputsDir, FILE_NAME_PROTO, TargetFileSuffix)
	return nil
}

func IsIncludeDir(dirName string) bool {
	if len(vIncludeDirs) == 0 {
		return true
	}
	for _, item := range vIncludeDirs {
		if item == dirName {
			return true
		}
	}
	return false
}
